// ==UserScript==
// @name         test - GM_xmlhttpRequest
// @namespace    http://tako.tampermonkey.net/
// @version      0.1
// @description  testFormula
// @author       tako
// @match        *://*/*
// @run-at       context-menu
// @grant        GM_xmlhttpRequest
// ==/UserScript==
(function () {
  'use strict';
  // Your code here...
  var token = localStorage.getItem('console_TOKEN');
  var param = JSON.parse(prompt('请输入参数:', ''));
  param.excelKey = prompt('请输入Excel坐标:', '');
  param.multiExpr = prompt('请输入公式:', '');

  // 方式1
  fetch('/ce/test/execMultiExpr', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Logintoken': token
    },
    body: JSON.stringify(param),
  })
  .then(response => response.text())
  .then(text => {
    console.log('成功获取数据:', text);
    tip('结果', text);
  })
  .catch(error => console.error('请求失败:', error));



  // 方式2, 使用GM_xmlhttpRequest异步
  execMultiExpr(token, param)
    .then(response => {
      tip('结果', response.responseText);
    });

  function execMultiExpr(token, param) {
    return new Promise((resolve, reject) => {
      GM_xmlhttpRequest({
        method: 'POST',
        url: '/ce/test/execMultiExpr',
        headers: {
          'Logintoken': token
        },
        data: JSON.stringify(param),
        onload: function (response) {
          if (response.status === 200) {
            resolve(response);
          } else {
            reject(err);
          }
        },
        onerror: function (err) {
          reject(err);
        }
      });
    });
  }


  // 方式3，使用GM_xmlhttpRequest
  GM_xmlhttpRequest({
    method: 'POST',
    url: '/ce/test/execMultiExpr',
    headers: {
      'Logintoken': token
    },
    data: JSON.stringify(param),
    onload: function (response) {
      if (response.status === 200) {
        tip('结果', response.responseText);
      }
    },
    onerror: function (err) {
      console.error('请求错误:', err);
    }
  });

  function tip(title, msg) {
    const htmlText = msg.replace(/\r?\n/g, '<br/>').replace(/[ ]/g, '&nbsp;');;
    // 创建弹窗元素
    let dialog = document.createElement('div');
    dialog.style.position = 'fixed';
    dialog.style.top = '50px';
    dialog.style.left = '50%';
    dialog.style.transform = 'translateX(-50%)';
    dialog.style.width = '800px';
    dialog.style.overflow = 'auto';
    dialog.style.background = '#fff';
    dialog.style.border = '1px solid #aaa';
    dialog.style.borderRadius = '8px';
    dialog.style.boxShadow = '0 4px 12px rgba(0,0,0,0.2)';
    dialog.style.zIndex = '99999';
    dialog.style.padding = '20px';
    dialog.style.fontFamily = 'sans-serif';
    dialog.style.textAlign = 'left';

    dialog.innerHTML = `
      <div style="display: flex;justify-content: space-between;align-items: center;padding-bottom: 10px;border-bottom: 1px solid #eee;margin-bottom: 15px;">
          <h3 style="font-size: 18px;font-weight: bold;margin: 0;">${title}</h3>
          <button style="font-size: 24px;font-weight: bold;cursor: pointer;padding: 0 5px;border: none;">&times;</button>
      </div>
      <div style="overflow: auto;margin-bottom: 15px;height: 800px;">${htmlText}</div>
    `;

    dialog.querySelector('button').addEventListener('click', () => {
      dialog.remove();
    });

    document.body.appendChild(dialog);
  }

})();